<template>
  <div>
    <div>
      <div>
        <el-button @click="openCategory('')" type="primary">新增</el-button>
      </div>
      <div class="top-operate-default mar-t-20 mar-b-20">
        <div class="search-item">
          <el-input @keyup.enter.native="handleSearch" v-model="dataListQuery.categoryName" placeholder="请输入分类名称" style="width: 200px"></el-input>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button plain @click="resetSearch">重置</el-button>
          </el-button-group>          
        </div>
      </div>

      <!-- 内容列表 -->
      <div class="table-container">
        <el-table :data="dataList" v-loading="listLoading" element-loading-text="加载中" style="width: 100%" >
          <el-table-column label="供应商分类名称">
            <template slot-scope="scope">
              <span @click="openCategory(scope.row)" class="cursor-p">{{ scope.row.categoryName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="描述" prop="description"></el-table-column>
          <el-table-column label="供应商数" prop="supplierCount"></el-table-column>
          <el-table-column width="140px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="openCategory(scope.row)">编辑</el-button>
              <el-button type="text" size="medium" @click="deleteCategory(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="dataListQuery.currentPage"
                       :page-sizes="[10, 20, 50, 100]" :page-size="dataListQuery.size"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>

      <!--供应商分类-->
      <el-dialog :title="categoryDialogTitile" :visible.sync="formVisible" :close-on-press-escape="true" width="650px">
        <el-form :model="categoryForm" label-position="right" label-width="100px" :rules="rules" ref="categoryForm" style='width: 500px; margin-left:50px;'>
          <el-form-item label="分类名称" prop="categoryName">
            <el-input v-model="categoryForm.categoryName"></el-input>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="categoryForm.description" type="textarea" :rows="3"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="formVisible = false">取消</el-button>
          <el-button type="primary" @click="doSave">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { pagingSupplierCategory, saveSupplierCategory, deleteSupplierCategory } from '@/api/user/supplierCategory'

  export default {
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      }
    },
    data() {
      return {
        // 分类表单
        categoryForm: {
          enterpriseId: this.getEnterpriseId(),
          categoryId: '',
          categoryName: '',
          description: ''
        },
        // 查询表单
        dataListQuery: {
          enterpriseId: this.getEnterpriseId(),
          categoryName: '',
          currentPage: 1,
          size: 10
        },
        rules: {
          categoryName: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
          ]
        },
        // dialog title
        categoryDialogTitile: '',
        total: null,
        dataList: [],
        listLoading: false,
        formVisible: false
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.initData()
        }
      }
    },
    // 初始化页面
    created() {
      this.initData()
    },
    mounted() {
    },
    methods: {
      // 初始数据
      initData() {
        this.listLoading = true
        pagingSupplierCategory(this.dataListQuery).then(response => {
          this.dataList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      // 查询
      handleSearch() {
        this.dataListQuery.currentPage = 1
        this.initData()
      },
      // 重置事件
      resetSearch() {
        this.dataListQuery = {
          enterpriseId: this.getEnterpriseId(),
          categoryName: '',
          currentPage: 1,
          size: 10
        }
        this.initData()
      },
      // 打开分类dialog事件
      openCategory(row) {
        if (row) {
          this.categoryDialogTitile = '编辑供应商分类'
          this.categoryForm = {
            enterpriseId: row.enterpriseId,
            categoryId: row.categoryId,
            categoryName: row.categoryName,
            description: row.description
          }
        } else {
          this.categoryDialogTitile = '新增供应商分类'
          this.categoryForm = {
            enterpriseId: this.getEnterpriseId(),
            categoryId: '',
            categoryName: '',
            description: ''
          }
        }
        this.formVisible = true
      },
      // 保存事件
      doSave() {
        this.$refs['categoryForm'].validate((valid) => {
          if (valid) {
            saveSupplierCategory(this.categoryForm).then(response => {
              if (response.isError) return
              this.formVisible = false
              this.$message({ type: 'success', message: '保存成功!' })
              this.initData()
            })
          }
        })
      },
      // 删除事件
      deleteCategory(row) {
        this.$confirm('此操作将删除该数据，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteSupplierCategory(row.categoryId).then(response => {
            if (response.isError) return
            this.initData()
          })
        }).catch(() => {})
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.dataListQuery.size = val
        this.initData()
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.dataListQuery.currentPage = val
        this.initData()
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
